﻿@using Newtouch.Infrastructure;
@{
    ViewBag.Title = "UnSettedFeeSettIndex";
    Layout = "~/Views/Shared/_Form.cshtml";
}

<div class="rows" style="margin-bottom: 1%;">
    <div class="panel panel-default" style="margin-bottom:0;">
        <div class="panel-heading navb-bg">
            住院患者信息
        </div>
        <table class="form" style="width:96%;">
            <tbody>
                <tr>
                    <th class="formTitle">住院号：</th>
                    <td class="formValue">
                        <input class="form-control" type="text" id="zyh" value="" />
                    </td>
                    <td class="formValue">
                        <input type="button" class="btn btn-default btn-md btn-default-color" title="选择住院患者" style="" id="zy_btnsyy" value="查询" onclick="GetPatSerarchView($('#zyh').val());">
                    </td>
                    <th class="formTitle">开始时间：</th>
                    <td class="formValue" style="width:113px;">
                        <label id="startTime"></label>
                    </td>
                    <th class="formTitle">结束时间：</th>
                    <td class="formValue" style="width:143px;">
                        <input id="endTime" type="text" class="form-control input-wdatepicker form-an formClearIgnore"
                               onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd HH:mm:ss', onpicked: function () { thenreload(); } })" value="@(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))" />
                    </td>
                    <th class="formTitle">姓名：</th>
                    <td class="formValue">
                        <label id="zy_xm"></label>
                    </td>
                    <th class="formTitle">性别：</th>
                    <td class="formValue">
                        <label id="zy_xb"></label>
                    </td>
                </tr>
            </tbody>
            <tbody class="dispTbody" style="display:none;">
                <tr>
                    <th class="formTitle">年龄：</th>
                    <td class="formValue" colspan="2">
                        <label id="zy_nl"></label>
                    </td>
                    <th class="formTitle">证件号：</th>
                    <td class="formValue">
                        <label id="zy_zjh"></label>
                    </td>
                    <th class="formTitle">入院诊断：</th>
                    <td class="formValue">
                        <label id="zy_ryzd"></label>
                    </td>
                    <th class="formTitle">出生日期：</th>
                    <td class="formValue">
                        <label id="zy_csny"></label>
                    </td>
                    <th class="formTitle">入院日期：</th>
                    <td class="formValue">
                        <label id="zy_ryrq"></label>
                    </td>
                </tr>
            </tbody>
        </table>
        <div class="fa_icontoggle"><i class="fa fa-angle-double-down icontoggle" style="color:rgb(0, 160, 233);cursor:pointer;"></i></div>
    </div>
</div>
<ul id="myTab" class="nav nav-tabs">
    <li class="active"><a href="#wj" data-toggle="tab">未结费用</a></li>
    <li><a href="#yj" data-toggle="tab">历史已结</a></li>
</ul>
<div id="myTabContent" class="tab-content">
    <div class="tab-pane fade in active" id="wj">
        <div class="gridPanel">
            <table id="gridList"></table>
        </div>
    </div>
    <div class="tab-pane fade" id="yj">
        <div class="gridPanel">
            <table id="gridList2"></table>
        </div>
        <div style="margin:3px 0;">
            <span style="color:red;font-weight:bold;">*.撤销功能限制为撤销列表中最后一次结算</span>
        </div>
    </div>
</div>

<div class="rows" style="margin-bottom: 1%;margin-top: 1%;">
    <div class="panel panel-default" style="margin-bottom:0;padding:10px;">
        <table class="form" style="width:96%;">
            <tbody>
                <tr>
                    <th class="formTitle">未结费用：</th>
                    <td class="formValue" style="width:100px;">
                        <label id="lblSumje" style="color:red;font-weight: bold;font-size: large;"></label>
                    </td>
                    <th class="formTitle" style="width:105px;">上次结算结束时间：</th>
                    <td class="formValue">
                        <label id="lastValidMidwaySettTime"></label>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
@Html.Partial("_BottomButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel()
{
    ShowKeyList = new int[] { 6, 8 },
    F8Text = "确认收费",
    F6Text = "撤销结算",
    F6Hidden = true,
    F8Hidden = true,
})
<script type="text/javascript">

    //通过ls把住院号传过去
    var fromzyh = sessionStorage.getItem('zyh');
    var defaultShow = sessionStorage.getItem('defaultShow');
    if (fromzyh) {
        $('#zyh').val(fromzyh);
        sessionStorage.removeItem('zyh');
    }
    if (defaultShow) {
        sessionStorage.removeItem('defaultShow');
    }

    $(function () {
        initgridList1();

        $('#myTab li:eq(1) a').tab('show'); //会触发grid2加载样式

        if (!(defaultShow === 'yj')) {
            setTimeout("$('#myTab li:eq(0) a').tab('show');", 200); //显示未结
        }

        if (!!$('#zyh').val()) {
            ajaxLoadDataResult();
        }
    });

    function initgridList1() {
        $('#gridList').dataGrid({
            unwritten: false,
            url: "/HospitalizationManage/BookkeepInHos/GetUnSettedFeeItemListJson",
            postData: { zyh: $("#zyh").val(), endTime: $("#endTime").val() },
            height: $(window).height() - 300,
            sortable: false,
            colModel: [
                { label: "收费项目名称", name: "sfxmmc", width: 180, align: "left" },
                {
                    label: "数量", name: "sl", width: 80, align: "left", formatter: "number"
                    , formatoptions: { decimalPlaces: 2, defaultValue: '0.00' }
                },
                {
                    label: "总金额", name: "zje", width: 80, align: "left", formatter: "number"
                        , formatoptions: { decimalPlaces: 2, defaultValue: '0.00' }
                },
            ],
            gridComplete: function (data) {
                var sumje = 0.00;
                $.each(data, function () {
                    sumje += this.zje;
                });
                if (sumje != 0) {
                    $('#lblSumje').html(sumje.toFixed(2));
                }
                else {
                    $('#lblSumje').html('');
                }
            }
        });
    }

    var flagIsFirstInitGrid2 = true;
    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
        var targetHref = $(e.target).attr('href');
        $('#btn_bottombutton_f6,#btn_bottombutton_f8').hide();
        if (targetHref && targetHref.indexOf('yj') != -1) {
            $('#btn_bottombutton_f6').show();
            if (flagIsFirstInitGrid2) {
                flagIsFirstInitGrid2 = false;
                initgridList2();
            }
            else {
                reloadGrid2();
            }
        }
        else {
            $('#btn_bottombutton_f8').show();
        }
    })

    function initgridList2() {
        $('#gridList2').dataGrid({
            unwritten: false,
            url: "/HospitalizationManage/BookkeepInHos/GetMidwaySettedListJson",
            postData: { zyh: $("#zyh").val() },
            height: $(window).height() - 300,
            sortable: false,
            colModel: [
                { label: "jsnm", name: "jsnm", width: 140, align: "left", hidden: true },
                { label: "住院号", name: "zyh", width: 140, align: "left", hidden: true },
                {
                    label: "结算金额", name: "zje", width: 80, align: "left", formatter: "number"
                    , formatoptions: { decimalPlaces: 2, defaultValue: '0.00' }
                },
                {
                    label: "结算开始时间", name: "jsksrq", width: 140, align: "left",
                    formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s' }
                },
                {
                    label: "结算结束时间", name: "jsjsrq", width: 140, align: "left",
                    formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s' }
                },
                { label: "结算人员", name: "CreatorCode", width: 100, align: "left" },
                {
                    label: "结算时间", name: "CreateTime", width: 140, align: "left",
                    formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s' }
                }
            ],
        });
    }

    function thenreload() {
        //加载未结
        reloadGrid();
        reloadLastValidMidwaySettTime();

        var targetHref = $('#myTab li.active a').attr('href');
        if (targetHref && targetHref.indexOf('yj') != -1) {
            reloadGrid2();  //当前已是已结
        }
    }

    function reloadGrid() {
        $("#gridList").jqGrid('setGridParam', {
            postData: { zyh: $("#zyh").val(), endTime: $("#endTime").val() },
        }).trigger('reloadGrid');

    }

    function reloadGrid2() {
        $("#gridList2").jqGrid('setGridParam', {
            postData: { zyh: $("#zyh").val() },
        }).trigger('reloadGrid');

    }

    function reloadLastValidMidwaySettTime() {
        //更新‘最后结算结束时间’
        $('#lastValidMidwaySettTime').html('');
        if (!!$('#zyh').val()) {
            $.najax({
                url: "/HospitalizationManage/BookkeepInHos/GetLastValidMidwaySettTimeJson?zyh=" + $("#zyh").val(),
                success: function (ajaxresp) {
                    if (ajaxresp.data) {
                        $('#lastValidMidwaySettTime').html($.getTime({ date: ajaxresp.data }));
                        $('#startTime').html($.getTime({ date: ajaxresp.data }));
                    }
                },
            });
        }
    }

    //病人查询
    function GetPatSerarchView(keyword) {
        var zyh = keyword;
        if (!!!keyword) {
            zyh = '';
        }
        $.modalOpen({
            id: "patSearch",
            title: "患者查询",
            url: "/PatientManage/AccountManage/PatSearchView?brzybzType=" + '@((int)EnumZYBZ.Bqz + "," + (int)EnumZYBZ.Djz)' + "&t=" + Math.random() + "&zyh=" + zyh,
            width: "700px",
            height: "600px",
            callBack: function (iframeId) {
                top.frames[iframeId].PatDbGrid(); //在弹出窗口事件
                //调用查询卡号和住院号
            }//窗口点确定的回调函数
        });

    }

    //选择的病人 callback
    function getPatInfoAjax(selePatInfo) {
        $('#endTime').val($.getTime()); //update endTime
        $('#zyh').val(selePatInfo.zyh);
        ajaxLoadDataResult();
    }

    //加载住院基本信息
    function ajaxLoadDataResult() {
        $('#startTime').html('');   //只有切换住院号的时候，才切换开始日期

        var zyh = $('#zyh').val();
        if (!!zyh) {
            $.najax({
                type: "GET",
                url: "/HospitalizationManage/BookkeepInHos/GetInpatientAccountingStatusDetail?zyh=" + zyh,
                loading: true,
                success: function (ajaxresp) {
                    setPatInfoModel(ajaxresp.data.patInfo);

                    thenreload();
                },
                errorCallback: function (err) {
                    newtouch_globalevent_f4();
                    $('#zyh').trigger('focus');
                }
            });
        }
        else {
            GetPatSerarchView();
        }
    }

    //初始化病人信息
    function setPatInfoModel(patModel) {
        $('#zy_xm').html(patModel.xm);
        $('#zy_xb').html($.getGender(patModel.xb));
        $('#zy_nl').html(getAgeFromBirthTime({ begin: patModel.csny }).text);
        $('#zy_ryzd').html(patModel.ryzdmc);
        $('#zy_zjh').html(patModel.zjh);
        $('#zy_phone').html(patModel.phone);
        $('#zy_csny').html(patModel.csny ? patModel.csny.substring(0, 10) : '');
        $('#zy_ryrq').html(patModel.ryrq ? patModel.ryrq.substring(0, 10) : '');

        $('#startTime').html(patModel.ryrq ? patModel.ryrq.substring(0, 10) : '');

    }

    function newtouch_event_f8() {
        var zyh = $('#zyh').val();
        var startTime = $('#startTime').html();
        if (!!!startTime) {
            $.modalAlert("未指定结算开始时间", 'error');
            return;
        }
        if (!!!zyh) {
            $.modalAlert("请选择住院病人", 'error');
            return;
        }
        var thisSettEndTime = $('#endTime').val();
        if (!!!thisSettEndTime) {
            $.modalAlert("未指定结算结束时间", 'error');
            return;
        }
        var allLine = $("#gridList").jqGrid('getRowData_AllLine', null, true);
        if (!!!allLine || allLine.length == 0) {
            $.modalAlert("无未结费用", 'error');
            return;
        }
        //提交
        var postData = { zyh: zyh, startTime: startTime, endTime: thisSettEndTime };
        $.submitForm({
            type: "POST",
            url: "/HospitalizationManage/BookkeepInHos/UnSettedItemFeeConfirm",
            param: postData,
            success: function (ajaxresp) {
                sessionStorage.setItem('zyh', $('#zyh').val());
                location.href = location.href;
            },
        });

    }

    function newtouch_event_f6() {
        var zyh = $('#zyh').val();
        if (!!!zyh) {
            $.modalAlert("请选择住院病人", 'error');
            return;
        }
        var allLine = $("#gridList2").jqGrid('getRowData_AllLine', null, true);
        if (!!!allLine || allLine.length == 0) {
            $.modalAlert("无历史已结", 'error');
            return;
        }
        //提交
        var postData = { zyh: zyh };
        $.submitForm({
            type: "POST",
            url: "/HospitalizationManage/BookkeepInHos/CancelTheLastMidwaySett",
            param: postData,
            success: function (ajaxresp) {
                sessionStorage.setItem('zyh', $('#zyh').val());
                sessionStorage.setItem('defaultShow', 'yj');
                location.href = location.href;
            },
        });

    }

</script>